define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdraw/index',
                    add_url: '',
                    edit_url: '',
                    del_url: 'withdraw/del',
                    multi_url: 'withdraw/multi',
                    table: 'withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'uid', title: __('Uid'), formatter: Controller.api.formatter.uid},
                        {field: 'type', title: __('Type'), formatter: Controller.api.formatter.type,searchList: {"1": "手动提现", "0": "自动提现"}},
                        {field: 'withdraw_name', title: __('Withdraw_name')},
                        {field: 'withdraw_account', title: __('Withdraw_account')},
                        {field: 'withdraw_type', title: __('Withdraw_type')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'realmoney', title: __('Realmoney'), operate:'BETWEEN'},
                        {field: 'percent', title: __('Percent'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),formatter: Controller.api.formatter.status,searchList: {"1": "成功", "2": "失败", "0": "处理中"}},
                        {field: 'back', title: __('Back'),formatter: Controller.api.formatter.back,operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),buttons: [
                            {
                                name: 'execute',
                                title: '已完成',
                                text: '已完成',
                                url: 'withdraw/status?status=1',
                                icon: '',
                                classname: 'btn btn-success btn-xs btn-execute btn-ajax',
                                success: function (data) {
                                    table.bootstrapTable('refresh');
                                    return false;
                                }
                            },
                            {
                                name: 'execute',
                                title: '处理中',
                                text: '处理中',
                                url: 'withdraw/status?status=0',
                                icon: '',
                                classname: 'btn btn-warning btn-xs btn-execute btn-ajax',
                                success: function (data) {
                                    table.bootstrapTable('refresh');
                                    return false;
                                }
                            },
                            {
                                name: 'execute',
                                title: '失败',
                                text: '失败', 
                                url: 'withdraw/status?status=2',
                                icon: '',
                                classname: 'btn btn-primary btn-xs btn-execute btn-ajax',
                                success: function (data, ret) {
                                    layer.prompt({
                                        title: '输入失败原因',
                                    },function(value, index, elem){
                                        layer.close(index);
                                        Fast.api.ajax({
                                            url: "withdraw/status?status=2&ids="+ret.id+"&back="+value,
                                        }, function (data, ret) {
                                            table.bootstrapTable('refresh');
                                        });
                                    });
                                    table.bootstrapTable('refresh');
                                    return false;
                                }
                            },
                        ],table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-type-1", function () {
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    return {
                        search: params.search,
                        sort: params.sort,
                        order: params.order,
                        filter: JSON.stringify({'type':1}),
                        op: JSON.stringify({'party':'='}),
                        offset: params.offset,
                        limit: params.limit,
                    };
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            $(document).on("click", ".btn-type-0", function () {
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    return {
                        search: params.search,
                        sort: params.sort,
                        order: params.order,
                        filter: JSON.stringify({'type':0}),
                        op: JSON.stringify({'party':'='}),
                        offset: params.offset,
                        limit: params.limit,
                    };
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            $(document).on("click", ".btn-go", function () {
                Fast.api.open('/admin/withdraw_auto/editt', __('今日手动体现'));
            });
            $(document).on("click", ".btn-goo", function () {
                Fast.api.open('/admin/withdraw_auto/edittt', __('昨日手动体现'));
            });
            

            $(document).on("click", ".btn-all", function () {
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    return {
                        search: params.search,
                        sort: params.sort,
                        order: params.order,
                        filter: JSON.stringify({}),
                        op: JSON.stringify({}),
                        offset: params.offset,
                        limit: params.limit,
                    };
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                type: function (value, row, index) {
                    return (value == 1 ? '手动' : '自动');
                },
                back: function (value, row, index) {
                    return (value == '' ? '无' : value);
                },
                status: function (value, row, index) {
                    if (value == 0) {
                        var i = '<span class="text-warning"><i class="fa fa-circle"></i> 处理中</span>';
                    }
                    if (value == 1) {
                        var i = '<span class="text-success"><i class="fa fa-circle"></i> 已完成</span>';
                    }
                    if (value == 2) {
                        var i = '<span class="text-danger"><i class="fa fa-circle"></i> 失败</span>';
                    }
                    return i;
                },
                uid: function (value, row, index) {
                    return (!value ? '无' : '<a data-original-title="用户信息（可编辑）" class="btn-execute btn-dialog" href="/admin/user/user/edit?ids='+value+'">'+value+'</a>');
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});